package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import javafx.concurrent.Service;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
      /**
     * 查询当前库存
     * 请求参数：Integer page, Integer rows, String codeOrName, Integer goodsTypeId
     */
    @PostMapping("/listInventory")
    public Map<String, Object> listInventory(Integer page,
                                             Integer rows,
                                             String codeOrName,
                                             Integer goodsTypeId) {

        return goodsService.listInventory(page, rows, codeOrName, goodsTypeId);
    }


    @PostMapping("/save")//http://localhost:8080/goods/save?goodsId=37
    public ServiceVO saveOrUpdate(Goods goods,
                                  HttpServletRequest request) {
        String goodsId = request.getParameter("goodsId");
        return goodsService.saveOrUpdate(goods, goodsId);
    }

    @PostMapping("/delete")//http://localhost:8080/goods/delete
    public ServiceVO delete(Integer goodsId) {
        return goodsService.deleteGoods(goodsId);
    }

    // 分页查询无库存商品列表
    @PostMapping("/getNoInventoryQuantity") //http://localhost:8080/goods/getNoInventoryQuantity
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        return goodsService.getNoInventoryQuantity(page, rows, nameOrCode);
    }

    // 分页查询有库存商品列表
    @PostMapping("/getHasInventoryQuantity") //http://localhost:8080/goods/getNoInventoryQuantity
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        return goodsService.getHasInventoryQuantity(page, rows, nameOrCode);
    }

    //添加库存、修改数量或成本价
    @PostMapping("/saveStock")//http://localhost:8080/goods/saveStock?goodsId=25
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        return goodsService.saveStock(goodsId, inventoryQuantity, purchasingPrice);
    }

    //删除库存
    @PostMapping("deleteStock")//http://localhost:8080/goods/deleteStock
    public ServiceVO deleteStock(Integer goodsId) {
        return goodsService.deleteStock(goodsId);
    }

    //查询所有 当前库存量 小于 库存下限的商品信息
    @PostMapping("/listAlarm")//http://localhost:8080/goods/listAlarm
    public Map<String, Object> listAlarm() {
        return this.goodsService.listAlarm();

    }

    //生成商品编码
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }
}
